package com.zhou.push;

import android.text.TextUtils;

import java.io.*;
import java.net.Socket;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by zhou on 2016/3/14 0014.
 */

public class IMWriter extends Thread {
    private LinkedBlockingQueue<String> blockingQueue = new LinkedBlockingQueue<String>();
    private OutputStream mOutput;
    public IMWriter(OutputStream output){
        this.mOutput = output;
    }
    @Override
    public void run() {
        super.run();
        if(mOutput!=null){
            while(true){
                try {
                    BufferedWriter mWriter =  new BufferedWriter(new OutputStreamWriter(mOutput));
                    String msg=blockingQueue.take();
                    mWriter.write(msg);
                    mWriter.newLine();
                    mWriter.flush();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println(this.getClass().getName()+" blockingQueue.take() 出问题了");
                    break;
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println(this.getClass().getName() + " mWriter.write(msg);\n" +
                            "                    mWriter.newLine();\n" +
                            "                    mWriter.flush(); 出问题了");
                    break;
                }
            }
        }
    }
    public boolean sendMsg(String msg){
        try {
            blockingQueue.put(msg);
            return true;
        } catch (InterruptedException e) {
            e.printStackTrace();
            return false;
        }
    }
}
